﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Awesomium.Core;
using System.Threading;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace Diplomova_prace
{
    public partial class Integration_win : Form
    {
        Integration_alg integral = new Integration_alg();
        Reg reg = new Reg();
        Expression expression = new Expression();
        Integrace_ret ret = new Integrace_ret();
        Methods method;
        string NameMethod;
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        string pathToHtml = string.Empty;
        readonly string temp = string.Empty;
        StreamWriter sw;
        static int instanceCounter;
        readonly int instanceId;
        string formula = string.Empty;
        string result = string.Empty;
        int defaultFontSize = 24;
        Exports export = new Exports();
        List<Control> alignedComponents = new List<Control>();
        bool incorrect = false;
        SaveDialogs save = new SaveDialogs();
        string PathToLastExportFile = string.Empty;
        string destination;
        string directory;
        bool showOutput = true;
        public enum Methods
        {
            MidPoint =0,
            Trapezodial =1,
            Simpson = 2,
            ThreeEighths = 3,
            Bool = 4,
            ComMidpoint = 5,
            ComTrapezodial = 6,
            ComSimpson = 7,
            ComThreeEighths = 8,
            ComBool = 9,
            RombergIntegration = 10,
            GaussLegendre = 11
        }

        public Integration_win(Methods method, string funkce = "", string dolni_mez = "", string horni_mez = "", string pocet_dilu = "", string pocet_bodu = "", string presnost = "")
        {
            this.method = method;
            this.instanceId = instanceCounter++;
            temp = MainMenu.tempProperty + "Integration";
            InitializeComponent();
            NameWindow();
            DisableComponents();
            AlignComponents();
            FontSize_trackBar.Value = defaultFontSize;
            this.Show();
            webControl1.Enabled = true;
            expression.incorrectInput += expression_incorrectInput;
            integral.post += integral_post;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.DoWork += new DoWorkEventHandler(DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Done);
            this.FillListChangeMethod(ChangeMethod_ListBox,MainMenu.dictIntTagName);
            funkce_TextBox.Text = funkce;
            dolniMez_TextBox.Text = dolni_mez;
            HorniMez_TextBox.Text = horni_mez;
            PocetDilu_textBox.Text = pocet_dilu;
            if (pocet_bodu != string.Empty)
                pocetBodu_numericUpDown.Value = Convert.ToDecimal(pocet_bodu);
            presnost_TextBox.Text = presnost;
            webControl1.PrintComplete += webControl1_PrintComplete;
        }

        void webControl1_PrintComplete(object sender, PrintCompleteEventArgs e)
        {
            toolStripStatusLabel1.Text = Path.GetFileName(Path.GetFileName(destination)) + " soubor byl vytvořen";
            PathToLastExportFile = destination;
            toolStripSplitButton1.Visible = true;
            string[] files = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\" + directory);
            foreach (string item in files)
            {
                    File.Copy(item, destination,true);
                    File.Delete(item);
            }
            Directory.Delete(Directory.GetCurrentDirectory() + "\\" +  directory);
        }
    
        void integral_post()
        {
            if (!incorrect)
            {
                MessageBox.Show("Nekorektní vstup", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                sw.Close();
                if (backgroundWorker1.IsBusy)
                    backgroundWorker1.CancelAsync();
                incorrect = true;
            }
        }

        void expression_incorrectInput()
        {
            if (!incorrect)
            {
                MessageBox.Show("Nekorektní vstup", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                sw.Close();
                incorrect = true;
            }
        }

        private void DoWork(object sender, DoWorkEventArgs e)
        {
            string a = expression.RPN(dolniMez_TextBox.Text);
            double b = expression.ComputeRPN(a);
            double dolniMez = expression.ComputeRPN(a);
            double horniMez = expression.ComputeRPN(expression.RPN(HorniMez_TextBox.Text));
            int pocetDilu = -1;
            double presnost = -1;

            if (PocetDilu_textBox.Text != string.Empty)
                pocetDilu = (int)expression.ComputeRPN(expression.RPN(PocetDilu_textBox.Text));


            if (method == Methods.MidPoint)
            {
                ret = integral.MidpointRule(funkce_TextBox.Text, dolniMez, horniMez);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowMidPoint(ret);
            }
            else if (method == Methods.Trapezodial)
            {
                ret = integral.TrapezodialRule(funkce_TextBox.Text, dolniMez, horniMez);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowTrapezodial(ret);
            }
            else if (method == Methods.Simpson)
            {
                ret = integral.SimpsonRule(funkce_TextBox.Text, dolniMez, horniMez);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowSimpson(ret);
            }
            else if (method == Methods.ThreeEighths)
            {
                ret = integral.ThreeEightsRule(funkce_TextBox.Text, dolniMez, horniMez);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowThreeEighths(ret);
            }
            else if (method == Methods.Bool)
            {
                ret = integral.BoolRule(funkce_TextBox.Text, dolniMez, horniMez);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowBool(ret);
            }
            else if (method == Methods.ComMidpoint)
            {
                ret = integral.ComMidpointRule(funkce_TextBox.Text, dolniMez, horniMez, pocetDilu);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowComMidpoint(ret);
            }
            else if (method == Methods.ComTrapezodial)
            {
                ret = integral.ComTrapezodialRule(funkce_TextBox.Text, dolniMez, horniMez, pocetDilu);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowComTrapezodial(ret);
            }
            else if (method == Methods.ComSimpson)
            {
                ret = integral.ComSimpsonRule(funkce_TextBox.Text, dolniMez, horniMez, pocetDilu);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowComSimpson(ret);
            }
            else if (method == Methods.ComThreeEighths)
            {
                ret = integral.ComThreeEights(funkce_TextBox.Text, dolniMez, horniMez, pocetDilu);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowComThreeEighths(ret);
            }
            else if (method == Methods.ComBool)
            {
                ret = integral.SlozeneBooleovoPravidlo(funkce_TextBox.Text, dolniMez, horniMez, pocetDilu);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowComBool(ret);
            }
            else if (method == Methods.RombergIntegration)
            {
                presnost = expression.ComputeRPN(expression.RPN(presnost_TextBox.Text));
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                ret = integral.RombergIntegration(funkce_TextBox.Text, dolniMez, horniMez, presnost);
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowRomberg(ret);
            }
            else if (method == Methods.GaussLegendre)
            {
                string fce = funkce_TextBox.Text;
                ret = integral.GaussQuadrature(fce, dolniMez, horniMez, pocetDilu, Convert.ToInt32(pocetBodu_numericUpDown.Value));
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    incorrect = true;
                    return;
                }
                result = ShowGaussLegendre(ret);
            }
            try
            {
                if (showOutput)
                {
                    sw.WriteLine(GenerateHtml(result));
                    sw.Close();
                }
            }
            catch { }
            
        }

        private void Done(object sender, RunWorkerCompletedEventArgs e)
        {
            VypocitejButton.Enabled = true;
            FontSize_trackBar.Value = defaultFontSize;
            DecimalPlaces_numericUpDown.Enabled = true;
            if (!incorrect)
                webControl1.Source = new Uri(Path.GetFullPath(pathToHtml));      
        }

        private void CheckFillTextBoxs()
        {
            if ((int)method < 5)
            {
                if (funkce_TextBox.Text != string.Empty && dolniMez_TextBox.Text != string.Empty && HorniMez_TextBox.Text != string.Empty)
                {
                    AddTask_Button.Enabled = true;
                    VypocitejButton.Enabled = true;
                }
                else
                {
                    AddTask_Button.Enabled = false;
                    VypocitejButton.Enabled = false;
               } 
            }
            if (((int)method >= 5 && (int) method < 10) || (int) method == 11)
            {
                if (funkce_TextBox.Text != string.Empty && dolniMez_TextBox.Text != string.Empty && HorniMez_TextBox.Text != string.Empty && 
                    PocetDilu_textBox.Text != string.Empty)
                {
                    AddTask_Button.Enabled = true;
                    VypocitejButton.Enabled = true;
                }
                else
                {
                    AddTask_Button.Enabled = false;
                    VypocitejButton.Enabled = false;
                }
            }
            if ((int)method == 10)
            {
                if (funkce_TextBox.Text != string.Empty && dolniMez_TextBox.Text != string.Empty && HorniMez_TextBox.Text != string.Empty && 
                    presnost_TextBox.Text != string.Empty)
                {
                    AddTask_Button.Enabled = true;
                    VypocitejButton.Enabled = true;
                }
                else
                {
                    AddTask_Button.Enabled = true;
                    VypocitejButton.Enabled = false;
                }
            }

        }

        private void VypocitejButton_Click(object sender, EventArgs e)
        {
            VypocitejButton.Enabled = false;
            DecimalPlaces_numericUpDown.Enabled = false;
            exportyToolStripMenuItem.Enabled = false;
            toolStripStatusLabel1.Text = string.Empty;
            toolStripSplitButton1.Visible = false;
            showOutput = true;
            RemoveHtmlInstance();
            incorrect = false;
            TimeSpan ts = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            pathToHtml = temp + instanceId + method.ToString() + ts.TotalSeconds.ToString() + ".html";
            
            this.sw = new StreamWriter(pathToHtml);
            if (CheckInput())
                sw.Close();
        }

        private bool CheckInput()
        {
            this.RemoveSpacesFromTextBoxs();
            string checkInfinity = this.CheckTextBoxsInfinity(expression);
            if (checkInfinity != string.Empty)
            {
                MessageBox.Show("Ve vstupních datech je nastavena nedefinovaná hodnota ("+ (checkInfinity)+")", "Nedefinovaná hodnota", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
       /*     if (!reg.Check(funkce_TextBox.Text))
            {
                MessageBox.Show("Špatně zadaná funkce", "Funkce", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }*/
            if (!reg.Check(dolniMez_TextBox.Text))
            {
                MessageBox.Show("Špatně zadaná dolní mez", "Dolní mez", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (!reg.Check(HorniMez_TextBox.Text))
            {
                MessageBox.Show("Špatně zadaná horní mez", "Horní mez", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (PocetDilu_textBox.Text != string.Empty)
                if (!reg.Check(PocetDilu_textBox.Text))
                {
                    MessageBox.Show("Nekorektní počet dílů", "Počet dílů", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    VypocitejButton.Enabled = true;
                    DecimalPlaces_numericUpDown.Enabled = true;
                    return true;
                }
            if (!funkce_TextBox.Text.Contains('x'))
            {
                MessageBox.Show("Musí být zadána funkce", "Funkce", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (dolniMez_TextBox.Text.Contains('x'))
            {
                MessageBox.Show("Musí být zadána číselná hodnota", "Dolní mez", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (HorniMez_TextBox.Text.Contains('x'))
            {
                MessageBox.Show("Musí být zadána číselná hodnota", "Horní mez", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (expression.ComputeRPN(expression.RPN(HorniMez_TextBox.Text)) <= expression.ComputeRPN(expression.RPN(dolniMez_TextBox.Text)))
            {
                MessageBox.Show("Horní mez musí být větší než dolní mez", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            double a = expression.ComputeRPN(expression.RPN(dolniMez_TextBox.Text));
            if (!reg.Check(presnost_TextBox.Text))
            {
                MessageBox.Show("Špatně zadaná přesnost", "Přesnost", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (presnost_TextBox.Text.Contains('x'))
            {
                MessageBox.Show("Musí být zadána číselná hodnota", "Přesnost", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (PocetDilu_textBox.Text.Contains('x'))
            {
                MessageBox.Show("Musí být zadána číselná hodnota", "Počet dílů", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            try 
            {
                double num = double.MinValue;
                if (PocetDilu_textBox.Text != string.Empty)
                    num = expression.ComputeRPN(expression.RPN(PocetDilu_textBox.Text));
                if (((int)method > 4 && (int)method < 10 || (int)method == 11) && (num != Math.Floor(num) || num <= 0))
                {
                    MessageBox.Show("Počet dílů musí být přirozené číslo", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    VypocitejButton.Enabled = true;
                    DecimalPlaces_numericUpDown.Enabled = true;
                    return true;
                }
                if (method == Methods.ComSimpson && num % 2 != 0)
                {
                    MessageBox.Show("Počet dílů musí být sudý", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    VypocitejButton.Enabled = true;
                    DecimalPlaces_numericUpDown.Enabled = true;
                    return true;
                }
                else if (method == Methods.ComBool && num % 4 != 0)
                {
                    MessageBox.Show("Počet dílů musí být dělitelný čtyřmi", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    VypocitejButton.Enabled = true;
                    DecimalPlaces_numericUpDown.Enabled = true;
                    return true;
                }
                else if (method == Methods.ComThreeEighths && num % 3 != 0)
                {
                    MessageBox.Show("Počet dílů musí být dělitelný třemi", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    VypocitejButton.Enabled = true;
                    DecimalPlaces_numericUpDown.Enabled = true;
                    return true;
                }
            }
            catch
            {
                MessageBox.Show("Počet dílů musí být přirozené číslo", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                VypocitejButton.Enabled = true;
                DecimalPlaces_numericUpDown.Enabled = true;
                return true;
            }
            if (presnost_TextBox.Text != string.Empty)
                if (expression.ComputeRPN(expression.RPN(presnost_TextBox.Text)) <= 0)
                {
                    MessageBox.Show("Počet dílů musí být kladná hodnota", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    VypocitejButton.Enabled = true;
                    DecimalPlaces_numericUpDown.Enabled = true;
                    return true;
                }
            backgroundWorker1.RunWorkerAsync();
            return false;
        }

        private void NameWindow()
        {
            if (method == Methods.MidPoint)
            {
                this.Text = "Obdélníkové pravidlo";
                formula = Formulas.Midpoint;
            }
            else if (method == Methods.Trapezodial)
            {
                this.Text = "Lichoběžníkové pravidlo";
                formula = Formulas.Trapezodial;
            }
            else if (method == Methods.Simpson)
            {
                this.Text = "Simpsonovo pravidlo";
                formula = Formulas.Simpson;
            }
            else if (method == Methods.ThreeEighths)
            {
                this.Text = "Tříosminové pravidlo";
                formula = Formulas.ThreeEighths;
            }
            else if (method == Methods.Bool)
            {
                this.Text = "Booleovo pravidlo";
                formula = Formulas.Bool;
            }
            else if (method == Methods.ComMidpoint)
            {
                this.Text = "Složené obdélníkové pravidlo";
                formula = Formulas.ComMidPoint;
            }
            else if (method == Methods.ComTrapezodial)
            {
                this.Text = "Složené lichoběžníkové pravidlo";
                formula = Formulas.ComTrapezodial;
            }
            else if (method == Methods.ComSimpson)
            {
                this.Text = "Složené Simpsonovo pravidlo";
                formula = Formulas.ComSimpson;
            }
            else if (method == Methods.ComThreeEighths)
            {
                this.Text = "Složené tříosminové pravidlo";
                formula = Formulas.ComThreeEighths;
            }
            else if (method == Methods.ComBool)
            {
                this.Text = "Složené Booleovo pravidlo";
                formula = Formulas.ComBool;
            }
            else if (method == Methods.RombergIntegration)
            {
                this.Text = "Rombergova integrace";
                formula = Formulas.RombergIntegration;
            }
            else if (method == Methods.GaussLegendre)
            {
                this.Text = "Gauss-Legendrova kvadratura";
                formula = Formulas.GaussLegendre;
            }
            NameMethod = this.Text;
        }

        private void DisableComponents()
        {
            if ((int)method < 5)
            {
                PocetDilu_label.Visible = false;
                PocetDilu_textBox.Visible = false;
                presnost_TextBox.Visible = false;
                presnost_label.Visible = false;
                PocetBodu_label.Visible = false;
                pocetBodu_numericUpDown.Visible = false;
            }
            else if ((int)method >= 5 && (int)method < 10)
            {
                presnost_TextBox.Visible = false;
                presnost_label.Visible = false;
                PocetBodu_label.Visible = false;
                pocetBodu_numericUpDown.Visible = false;
            }
            else if ((int)method == 10)
            {
                PocetDilu_label.Visible = false;
                PocetDilu_textBox.Visible = false;
                PocetBodu_label.Visible = false;
                pocetBodu_numericUpDown.Visible = false;
            }
            else
            {
                presnost_TextBox.Visible = false;
                presnost_label.Visible = false;
            }
            
        }

        private string ShowMidPoint(Integrace_ret input)
        {
            sb1.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(Mathml.LZ + input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-") + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + input.upperBoundaryProperty.NumToMathml("+","+"), "<mn>2</mn>") + Mathml.PZ);
            sb1.Append(Mathml.LineBreak);
            sb1.Append(input.widthProperty.NumToMathml() + Mathml.Multiply + input.fxProperty[0].Bracket());
            sb1.Append(Mathml.Equal + input.returnProperty.NumToMathml());
            sb1.Append(Mathml.EndMath);
            return sb1.ToString();
        }

        private string ShowTrapezodial(Integrace_ret input)
        {
            sb1.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(Mathml.LZ + Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), Mathml.Constant(2)) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.LZ + Mathml.Literal("f") + Mathml.LZ + input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.Plus + Mathml.Literal("f") + Mathml.LZ + input.upperBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.PZ);
            sb1.Append(Mathml.LineBreak);
            sb1.Append(input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ);
            sb1.Append(input.fxProperty[0].NumToMathml() + input.fxProperty[1].NumToMathml("+","+"));
            sb1.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml() + Mathml.EndMath);
            return sb1.ToString();
        }

        private string ShowSimpson(Integrace_ret input)
        {
            sb1.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(Mathml.LZ + Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), Mathml.Constant(6)) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.LZ + Mathml.Literal("f") + Mathml.LZ + input.lowerBoundaryProperty.NumToMathml(null, string.Empty) +
                Mathml.PZ + Mathml.Plus + Mathml.Constant(4) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ +
                Mathml.Frac(input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + input.upperBoundaryProperty.NumToMathml("+", "+"), Mathml.Constant(2)) +
                Mathml.PZ + Mathml.Plus + Mathml.Literal("f") + Mathml.LZ + input.upperBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.PZ);
            sb1.Append(Mathml.LineBreak);
            sb1.Append(input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ);
            sb1.Append(input.fxProperty[0].NumToMathml() + Mathml.Plus + Mathml.Constant(4) + Mathml.Multiply + input.fxProperty[1].Bracket() + input.fxProperty[2].NumToMathml("+","+"));
            sb1.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml() + Mathml.EndMath);
            return sb1.ToString();
        }

        private string ShowThreeEighths(Integrace_ret input)
        {
            sb1.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(Mathml.LZ + Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), Mathml.Constant(8)) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.LZ + Mathml.Literal("f") + Mathml.LZ + input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.Plus +
                        Mathml.Constant(3) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(Mathml.Constant(2) + Mathml.Multiply + input.lowerBoundaryProperty.Bracket() + input.upperBoundaryProperty.NumToMathml("+", "+"), Mathml.Constant(3)) + Mathml.PZ);
            sb1.Append(Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.Plus + Mathml.Constant(2) + Mathml.Multiply + input.upperBoundaryProperty.Bracket(), Mathml.Constant(3)) + Mathml.PZ + Mathml.Plus);
            sb1.Append(Mathml.Literal("f") + Mathml.LZ + input.upperBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.PZ);
            sb1.Append(Mathml.LineBreak);
            sb1.Append(input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ);
            sb1.Append(input.fxProperty[0].NumToMathml() + Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply);
            sb1.Append(input.fxProperty[1].Bracket());
            sb1.Append(Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply);
            sb1.Append(input.fxProperty[2].Bracket() + input.fxProperty[3].NumToMathml("+","+"));
            sb1.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml() + Mathml.EndMath);
            return sb1.ToString();
        }

        private string ShowBool(Integrace_ret input)
        {
            sb1.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(Mathml.LZ + Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), Mathml.Constant(90)) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.LZ + "<mn>7</mn>" + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.Plus +
                Mathml.Constant(32) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ);
            sb1.Append(Mathml.Frac(Mathml.Constant(3) + Mathml.Multiply + input.lowerBoundaryProperty.Bracket() + input.upperBoundaryProperty.NumToMathml("+", "+"), Mathml.Constant(4)) + Mathml.PZ);
            sb1.Append(Mathml.Plus + Mathml.Constant(12) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + input.upperBoundaryProperty.NumToMathml("+", "+"), Mathml.Constant(2)) + Mathml.PZ);
            sb1.Append(Mathml.Plus + Mathml.Constant(32) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ);
            sb1.Append(Mathml.Frac(input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply + input.upperBoundaryProperty.Bracket(), Mathml.Constant(4)) + Mathml.PZ + Mathml.Plus);
            sb1.Append(Mathml.Constant(7) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + input.upperBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.PZ);
            sb1.Append(Mathml.LineBreak);
            sb1.Append(input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ);
            sb1.Append(Mathml.Constant(7) + Mathml.Multiply);
            sb1.Append(input.fxProperty[0].Bracket());
            sb1.Append(Mathml.Plus + Mathml.Constant(32) + Mathml.Multiply);
            sb1.Append(input.fxProperty[1].Bracket());
            sb1.Append(Mathml.Plus + Mathml.Constant(12) + Mathml.Multiply);
            sb1.Append(input.fxProperty[2].Bracket());
            sb1.Append(Mathml.Plus + Mathml.Constant(32) + Mathml.Multiply);
            sb1.Append(input.fxProperty[3].Bracket());
            sb1.Append(Mathml.Plus + Mathml.Constant(7) + Mathml.Multiply);
            sb1.Append(input.fxProperty[4].Bracket());
            sb1.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml());
            sb1.Append(Mathml.EndMath);
            return sb1.ToString();
        }

        private string ShowComMidpoint(Integrace_ret input)
        {
            sb1.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(Mathml.LZ + Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), input.numOfPartProperty.NumToMathml()) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.HLZ);
            for (int i = 1; i < input.xProperty.Count; i++)
            {
                sb1.Append(Mathml.Literal("f") + Mathml.LZ + Mathml.Frac((input.xProperty[i - 1] - input.widthProperty / 2.0).NumToMathml(null, string.Empty) + (input.xProperty[i] - input.widthProperty / 2.0).NumToMathml("+", "+"), Mathml.Constant(2)) + Mathml.PZ);
                if (i != input.xProperty.Count - 1)
                    sb1.Append(Mathml.Plus);
                else
                {
                    sb1.Append(Mathml.Plus);
                    sb1.Append(Mathml.Literal("f") + Mathml.LZ + Mathml.Frac((input.xProperty[i] - input.widthProperty / 2.0).NumToMathml(null, string.Empty) + (input.xProperty[i] + input.widthProperty / 2.0).NumToMathml("+", "+"), Mathml.Constant(2)) + Mathml.PZ);
                }
            }
            sb1.Append(Mathml.HPZ);
            sb1.Append(Mathml.LineBreak);
            sb1.Append(Mathml.EndMath);
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ);
            for (int i = 0; i < input.fxProperty.Count; i++)
            {
                if (i > 0)
                    sb1.Append(input.fxProperty[i].NumToMathml("+", "+"));
                else
                    sb1.Append(input.fxProperty[i].NumToMathml());
            }
            sb1.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml());
            sb1.Append(Mathml.EndMath);
            return sb1.ToString();
        }

        private string ShowComTrapezodial(Integrace_ret input)
        {
            sb1.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(Mathml.LZ + Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), Mathml.Constant(2)+ Mathml.Multiply + input.numOfPartProperty.NumToMathml()) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.HLZ + Mathml.Literal("f") + Mathml.LZ + input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ);
            for (int i = 1; i < input.xProperty.Count - 1; i++)
            {
                sb1.Append(Mathml.Plus + Mathml.Constant(2) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + input.xProperty[i].NumToMathml(null, string.Empty) + Mathml.PZ);
            }
            sb1.Append(Mathml.Plus + Mathml.Literal("f") + Mathml.LZ + input.upperBoundaryProperty.NumToMathml(null, "+") + Mathml.PZ + Mathml.HPZ);
            sb1.Append(Mathml.LineBreak);
            sb1.Append(Mathml.EndMath);
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ);
            sb1.Append(input.fxProperty[0].NumToMathml());
            for (int i = 1; i < input.fxProperty.Count - 1; i++)
            {
                sb1.Append(input.fxProperty[i].NumToMathml("+", "+"));
            }
            sb1.Append(Mathml.Plus + input.fxProperty[input.fxProperty.Count - 1].NumToMathml());
            sb1.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml() + Mathml.EndMath);
            return sb1.ToString();
        }

        private string ShowComSimpson(Integrace_ret input)
        {
            sb1.Clear();
            sb2.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb2.Append(Mathml.HeaderMath() + input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ + input.fxProperty[0].NumToMathml());
            sb1.Append(Mathml.LZ + Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), Mathml.Constant(3) + Mathml.Multiply + input.numOfPartProperty.NumToMathml()) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.LZ);
            sb1.Append(Mathml.Literal("f") + Mathml.LZ + input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ);
            for (int i = 1; i < input.xProperty.Count - 1; i++)
            {
                if (i % 2 == 1)
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(4) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(input.xProperty[i - 1].NumToMathml() + input.xProperty[i + 1].NumToMathml("+", "+"), Mathml.Constant(2)) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(4) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
                else
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(2) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + input.xProperty[i].NumToMathml(null, string.Empty) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(2) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
            }
            sb1.Append(Mathml.Plus + Mathml.Literal("f") + Mathml.LZ + input.upperBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.PZ);
            sb1.Append(Mathml.LineBreak + Mathml.EndMath);
            sb2.Append(input.fxProperty[input.fxProperty.Count - 1].NumToMathml("+", "+"));
            sb2.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml() + Mathml.EndMath);
            return sb1.ToString() + sb2.ToString();
        }

        private string ShowComBool(Integrace_ret input)
        {
            sb1.Clear();
            sb2.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb2.Append(Mathml.HeaderMath() + input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ + input.fxProperty[0].NumToMathml());
            sb1.Append(Mathml.LZ + Mathml.Frac(Mathml.Constant(2) + Mathml.Multiply + Mathml.LZ + input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-") + Mathml.PZ, Mathml.Constant(45) + Mathml.Multiply + input.numOfPartProperty.NumToMathml()) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.LZ);
            sb1.Append(Mathml.Constant(7) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ);
            for (int i = 1; i < input.xProperty.Count - 1; i++)
            {
                if (i % 4 == 1)
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(32) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(Mathml.Constant(3) + Mathml.Multiply + input.xProperty[i - 1].Bracket() + input.xProperty[i + 3].NumToMathml("+", "+"), Mathml.Constant(4)) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(32) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
                else if (i % 4 == 3)
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(32) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(input.xProperty[i - 3].NumToMathml() + Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply + input.xProperty[i + 1].Bracket(), Mathml.Constant(4)) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(32) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
                else if (i % 4 == 2)
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(12) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(input.xProperty[i - 2].NumToMathml() + input.xProperty[i + 2].NumToMathml("+", "+"), Mathml.Constant(2)) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(12) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
                else
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(14) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + input.xProperty[i].NumToMathml(null, string.Empty) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(14) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
            }
            sb1.Append(Mathml.Plus + Mathml.Constant(7) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + input.upperBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.PZ);
            sb1.Append(Mathml.LineBreak + Mathml.EndMath);
            sb2.Append(input.fxProperty[input.fxProperty.Count - 1].NumToMathml("+", "+"));
            sb2.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml() + Mathml.EndMath);
            return sb1.ToString() + sb2.ToString();
        }

        private string ShowComThreeEighths(Integrace_ret input)
        {
            sb1.Clear();
            sb2.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb2.Append(Mathml.HeaderMath() + input.widthProperty.NumToMathml() + Mathml.Multiply + Mathml.LZ + input.fxProperty[0].NumToMathml());
            sb1.Append(Mathml.LZ + Mathml.Frac(Mathml.Constant(3) + Mathml.Multiply + Mathml.LZ + input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-") + Mathml.PZ, Mathml.Constant(8) + Mathml.Multiply + input.numOfPartProperty.NumToMathml()) + Mathml.PZ);
            sb1.Append(Mathml.Multiply + Mathml.LZ);
            sb1.Append(Mathml.Literal("f") + Mathml.LZ + input.lowerBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ);
            for (int i = 1; i < input.xProperty.Count - 1; i++)
            {
                if (i % 3 == 1)
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(Mathml.Constant(2) + Mathml.Multiply + input.xProperty[i - 1].Bracket() + input.xProperty[i + 2].NumToMathml("+", "+"), Mathml.Constant(3)) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
                else if (i % 3 == 2)
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + Mathml.Frac(input.xProperty[i - 2].NumToMathml() + Mathml.Plus + Mathml.Constant(2) + Mathml.Multiply + input.xProperty[i + 1].Bracket(), Mathml.Constant(3)) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(3) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
                else
                {
                    sb1.Append(Mathml.Plus + Mathml.Constant(2) + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ + input.xProperty[i].NumToMathml(null, string.Empty) + Mathml.PZ);
                    sb2.Append(Mathml.Plus + Mathml.Constant(2) + Mathml.Multiply + input.fxProperty[i].Bracket());
                }
            }
            sb1.Append(Mathml.Plus + Mathml.Literal("f") + Mathml.LZ + input.upperBoundaryProperty.NumToMathml(null, string.Empty) + Mathml.PZ + Mathml.PZ);
            sb1.Append(Mathml.LineBreak + Mathml.EndMath);
            sb2.Append(input.fxProperty[input.fxProperty.Count - 1].NumToMathml("+", "+"));
            sb2.Append(Mathml.PZ + Mathml.Equal + input.returnProperty.NumToMathml() + Mathml.EndMath);
            return sb1.ToString() + sb2.ToString();
        }

        private string ShowGaussLegendre(Integrace_ret input)
        {
            int cnt = 0;
            sb1.Clear();
            sb2.Clear();
            sb1.Append(Mathml.HeaderMath());
            sb1.Append(Mathml.LZ + Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), Mathml.Constant(2) + Mathml.Multiply + input.numOfPartProperty.NumToMathml()) + Mathml.PZ);
            sb1.Append(Mathml.Multiply);
            sb2.Append(Mathml.HeaderMath()+input.widthProperty.NumToMathml() + Mathml.Multiply);
            sb1.Append(Mathml.LZ);
            sb2.Append(Mathml.LZ);
            for (int i = 0; i < input.numOfPartProperty; i++)
            {
                if (i != 0)
                {
                    sb1.Append(Mathml.Plus);
                    sb2.Append(Mathml.Plus);
                }
                for (int j = 0; j < input.GaussPointProperty.Count; j++)
                {
                    if (j != 0)
                    {
                        sb1.Append(Mathml.Plus);
                        sb2.Append(Mathml.Plus);
                    }
                    sb1.Append(Mathml.HLZ + input.GaussWeightProperty[j].NumToMathml() + Mathml.Multiply + Mathml.Literal("f") + Mathml.LZ +
                        Mathml.Frac(Mathml.Constant(1), Mathml.Constant(2)) + Mathml.Multiply + input.GaussPointProperty[j].Bracket() + Mathml.Multiply + Mathml.LZ +
                        Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), input.numOfPartProperty.NumToMathml()) + Mathml.PZ +
                        input.lowerBoundaryProperty.NumToMathml("+", "+") + Mathml.Plus + Mathml.LZ +
                        Mathml.Frac(input.upperBoundaryProperty.NumToMathml() + (-1 * input.lowerBoundaryProperty).NumToMathml("+", "-"), input.numOfPartProperty.NumToMathml()) + Mathml.PZ +
                        Mathml.Multiply + Mathml.LZ + Mathml.Constant(i) + Mathml.Plus + Mathml.Frac(Mathml.Constant(1), Mathml.Constant(2)) + Mathml.PZ + Mathml.PZ + Mathml.HPZ);
                    sb2.Append(Mathml.HLZ + input.GaussWeightProperty[j].NumToMathml() + Mathml.Multiply + input.fxProperty[j+cnt*input.GaussPointProperty.Count].Bracket() + Mathml.HPZ);
                }
                cnt++;

            }
            sb1.Append(Mathml.PZ);
            sb2.Append(Mathml.PZ);
            sb1.Append(Mathml.LineBreak + Mathml.EndMath);
            sb2.Append(Mathml.Equal + input.returnProperty.NumToMathml());
            sb2.Append(Mathml.EndMath);
            return sb1.ToString() + sb2.ToString();
            
        }

        private string ShowRomberg(Integrace_ret input)
        { 
            return (Mathml.HeaderMath() + Mathml.Table(input.RombergProperty) + Mathml.EndMath);
        }

        private void Integration_win_FormClosing(object sender, FormClosingEventArgs e)
        {
            RemoveHtmlInstance();   
        }

        private string GenerateHtml(string input)
        {
            string currDirectory = Directory.GetCurrentDirectory();
            string dolniMez = (expression.TextToMathml(dolniMez_TextBox.Text, Mathml.Display.inline)).Replace(Mathml.HeaderMath(Mathml.Display.inline),string.Empty);
            dolniMez = dolniMez.Replace(Mathml.EndMath,string.Empty);
            
            string horniMez = (expression.TextToMathml(HorniMez_TextBox.Text, Mathml.Display.inline)).Replace(Mathml.HeaderMath(Mathml.Display.inline), string.Empty);
            horniMez = horniMez.Replace(Mathml.EndMath, string.Empty);

            StringBuilder divInput = new StringBuilder(Html.Div() + Html.Span("Byla zadána funkce:    "));
            divInput.Append(Mathml.HeaderMath(Mathml.Display.inline));
            divInput.Append(Mathml.Literal("f") + Mathml.LZ + Mathml.Literal("x") + Mathml.PZ + Mathml.Equal + Mathml.Space() + Mathml.EndMath);
            divInput.Append(expression.TextToMathml(funkce_TextBox.Text, Mathml.Display.inline));
            divInput.Append(Html.Span(".")+Html.Br+Html.Span(" Úkolem je určit hodnotu integrálu na intervalu:    "));
            divInput.Append(Mathml.HeaderMath(Mathml.Display.inline) + Mathml.TypeBracket.hranate.ToDescription() +dolniMez+Mathml.Text(";")  + horniMez);
            divInput.Append((Mathml.EndMfenced + Mathml.EndMath + Html.Span(".") + Html.Br + Html.Span(" Zvolená metoda:   ") + Html.Span(NameMethod) + Html.Br));
            divInput.Append(Html.Span("Předpis zvolené metody:")+Html.Br);
            divInput.Append(formula + Html.Br);
            if (((int)method > 4 && (int)method < 10)|| (int) method==11)
                divInput.Append(Html.Span("Počet dílů, na který bude interval rozdělen:   ")+ expression.TextToMathml(PocetDilu_textBox.Text,Mathml.Display.inline)+Html.Br);
            if (((int)method > 4 && (int)method < 10))
                divInput.Append(Html.Span("Krok") + Mathml.HeaderMath(Mathml.Display.inline) + Mathml.Space() +  Mathml.Literal("h") + Mathml.Space() +Mathml.EndMath + Html.Span("metody : ") + Mathml.HeaderMath(Mathml.Display.inline) + expression.TextToMathml((string) "(" +(HorniMez_TextBox.Text + "-" + dolniMez_TextBox.Text) + ")/" + PocetDilu_textBox.Text, Mathml.Display.inline) + Mathml.Equal +Mathml.Space()+ expression.ComputeRPN(expression.RPN((string) "(" +(HorniMez_TextBox.Text + "-" + dolniMez_TextBox.Text) + ")/" + PocetDilu_textBox.Text)).NumToMathml() + Mathml.EndMath + Html.Br);
            if (method == Methods.RombergIntegration)
                divInput.Append(Html.Span(" Hodnota integrálu se bude počítat s přesností:    ") + expression.TextToMathml(presnost_TextBox.Text, Mathml.Display.inline) + Html.Br);
            
            if (method == Methods.GaussLegendre)
                divInput.Append(Html.Span("Počet bodů Gauss-Legendrovy kvadratury:    ") + expression.TextToMathml(pocetBodu_numericUpDown.Value.ToString(), Mathml.Display.inline) + Html.Br);
            divInput.Append(Html.Span("Výsledek")+Html.Br+Html.EndDiv);
            input = Html.Div() + input + Html.EndDiv;
            result = Html.HeaderHTML(NameMethod) + divInput.ToString() + input + Html.EndHTML;
            return result;
        }
        
        private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
        {
            Match match = Regex.Match(result, "font-size:\\s*[0-9]*px");
            result = result.Replace(match.ToString(), "font-size:"+FontSize_trackBar.Value+"px");
            this.sw = new StreamWriter(pathToHtml);
            sw.WriteLine(result);
            sw.Close();
            webControl1.Source = new Uri(Path.GetFullPath(pathToHtml));    
        }

        private void RemoveHtmlInstance()
        {
            string directory = Directory.GetCurrentDirectory();
            List<string> files = Directory.GetFiles(directory).ToList();
            foreach (string file in files)
            {
                if (file.Contains(temp + instanceId + method.ToString()))
                {
                    File.Delete(Path.GetFullPath(file));
                }
            }
        }

        private void AlignComponents()
        {
            int centerPanel = splitContainer1.Panel1.Width / 2;
            if ((int)method < 5)
                VypocitejButton.Location = new Point(centerPanel-VypocitejButton.Size.Width/2, PocetDilu_textBox.Location.Y);
            else if ((int) method >= 5 && (int) method < 10)
                VypocitejButton.Location = new Point(centerPanel - VypocitejButton.Size.Width / 2, presnost_TextBox.Location.Y);
            else if ((int)method == 10)
            {
                VypocitejButton.Location = new Point(centerPanel - VypocitejButton.Size.Width / 2, presnost_TextBox.Location.Y);
                presnost_TextBox.Location = PocetDilu_textBox.Location;
                presnost_label.Location = PocetDilu_label.Location;
            }
            else
            {
                VypocitejButton.Location = new Point(centerPanel - VypocitejButton.Size.Width / 2, pocetBodu_numericUpDown.Location.Y);
                pocetBodu_numericUpDown.Location = presnost_TextBox.Location;
                PocetBodu_label.Location = presnost_label.Location;
            }
        }

        private void xMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (save.SaveAsXML(result))
            {
                toolStripStatusLabel1.Text = Path.GetFileName(save.PathToDestinationFileProperty) + " soubor byl vytvořen";
                PathToLastExportFile = save.PathToDestinationFileProperty;
                toolStripSplitButton1.Visible = true;
            }
        }

        private void hTMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (save.SaveAsHTML(result))
            {
                toolStripStatusLabel1.Text = Path.GetFileName(save.PathToDestinationFileProperty) + " soubor byl vytvořen";
                PathToLastExportFile = save.PathToDestinationFileProperty;
                toolStripSplitButton1.Visible = true;
            }
        }

        private void imageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (result != string.Empty)
            {
                int width = int.Parse(webControl1.ExecuteJavascriptWithResult("WidthDiv()"));
                int height = int.Parse(webControl1.ExecuteJavascriptWithResult("HeightDiv()"));
                int fontSize = FontSize_trackBar.Value;
                if (save.SaveAsImage(result, width, height, fontSize, pathToHtml))
                {
                    toolStripStatusLabel1.Text = Path.GetFileName(save.PathToDestinationFileProperty) + " soubor byl vytvořen";
                    PathToLastExportFile = save.PathToDestinationFileProperty;
                    toolStripSplitButton1.Visible = true;
                }
            }
        }

        private void FontSize_trackBar_MouseUp(object sender, MouseEventArgs e)
        {
            string size = FontSize_trackBar.Value.ToString();
            webControl1.ExecuteJavascript("changeSize('" + size + "px')");
        }

        private void FontSize_trackBar_KeyUp(object sender, KeyEventArgs e)
        {
            string size = FontSize_trackBar.Value.ToString();
            webControl1.ExecuteJavascript("changeSize('" + size + "px')");
        }

        private void pDFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (result != string.Empty)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Portable Document Format (*.pdf)| *.pdf";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    destination = sfd.FileName;
                    if (!GlobalMethod.FileInUse(destination))
                    {
                        directory = Path.GetFileName(pathToHtml).Replace(Path.GetExtension(pathToHtml), string.Empty);
                        Directory.CreateDirectory(directory);
                        webControl1.PrintToFile(directory, PrintConfig.Default);
                    }
                    else
                    {
                        MessageBox.Show("Proces nemůže přistupovat k souboru "+destination+", protože je využíván jiným procesem", "Soubor je otevřen", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void DecimalPlaces_ValueChanged(object sender, EventArgs e)
        {
            Extension.countDecimalPlaces = (int)DecimalPlaces_numericUpDown.Value;
        }

        private void ChangeMethod_ListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Integration_win integration;
            if (ChangeMethod_ListBox.SelectedIndex > -1)
                integration = new Integration_win((Methods)ChangeMethod_ListBox.SelectedIndex, 
                    (funkce_TextBox.Visible)? funkce_TextBox.Text : string.Empty,
                    (dolniMez_TextBox.Visible)? dolniMez_TextBox.Text :string.Empty,
                    (HorniMez_TextBox.Visible)? HorniMez_TextBox.Text :string.Empty,
                    (PocetDilu_textBox.Visible)? PocetDilu_textBox.Text : string.Empty,
                    (pocetBodu_numericUpDown.Visible) ? pocetBodu_numericUpDown.Value.ToString() : string.Empty,
                    (presnost_TextBox.Visible) ? presnost_TextBox.Text: string.Empty);
        }

        private void AddTask_Button_Click(object sender, EventArgs e)
        {
            showOutput = false;
            if (!CheckInput())
                if (!incorrect)
                    AddTask();
                else
                    MessageBox.Show("Úloha není korektně zadána", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }

        private void AddTask()
        {           
            DateTime date = DateTime.Now;
            string file = date.ToString("yyyyMMddHHmmss") + ".xml";
            string[] Files = Directory.GetFiles(Directory.GetCurrentDirectory() + MainMenu.PathTaskIntProperty + MainMenu.dictIntTagName[(int)method]);
            string path = Directory.GetCurrentDirectory() + MainMenu.PathTaskIntProperty + MainMenu.dictIntTagName[(int)method] + "\\" + file;
            StreamWriter stw = new StreamWriter(path);
            stw.WriteLine("<?xml version='1.0' encoding='UTF-8'?>");
            stw.WriteLine("<uloha>");
            stw.WriteLine("<funkce>"  + funkce_TextBox.Text + "</funkce>");
            stw.WriteLine("<dolni_mez>" + dolniMez_TextBox.Text + "</dolni_mez>");
            stw.WriteLine("<horni_mez>" + HorniMez_TextBox.Text + "</horni_mez>");
            if (PocetDilu_textBox.Visible && PocetDilu_textBox.Text != string.Empty) 
                stw.WriteLine("<pocet_dilu>" + PocetDilu_textBox.Text + "</pocet_dilu>");
            if (pocetBodu_numericUpDown.Visible && pocetBodu_numericUpDown.Visible)
                stw.WriteLine("<pocet_bodu>" + pocetBodu_numericUpDown.Value.ToString() + "</pocet_bodu>");
            if (presnost_TextBox.Visible && presnost_TextBox.Text != string.Empty)
                stw.WriteLine("<presnost>" + presnost_TextBox.Text + "</presnost>");
            stw.WriteLine("</uloha>");
            stw.Close();
            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + MainMenu.PathTaskIntProperty + MainMenu.dictIntTagName[(int)method] + "\\" + file);
            string tmp = sr.ReadToEnd().Trim();
            foreach (string item in Files)
            {
                StreamReader str = new StreamReader(item);
             
                string tmp1 = str.ReadToEnd().Trim();
                if (tmp == tmp1)
                {
                    MessageBox.Show("Tato úloha byla již přidána","Existující úloha",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                    str.Close();
                    sr.Close();
                    File.Delete(Directory.GetCurrentDirectory() + MainMenu.PathTaskIntProperty + MainMenu.dictIntTagName[(int)method] + "\\" + file);
                    return;
                }
                str.Close();
            }
            sr.Close();
            
            toolStripStatusLabel1.Text = "Úloha byla přidána do cvičných úloh";

        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
            Process.Start(PathToLastExportFile);   
        }

        private void Awesomium_Windows_Forms_WebControl_DocumentReady(object sender, UrlEventArgs e)
        {
            FontSize_trackBar.Enabled = true;
            while (!webControl1.ExecuteJavascriptWithResult("IsCompleted()")) ;
            exportyToolStripMenuItem.Enabled = true;           
        }

        private void toolStripSplitButton1_MouseHover(object sender, EventArgs e)
        {
            statusStrip.ShowItemToolTips = true;
            toolStripSplitButton1.ToolTipText = "otevřít soubor";
        }

        private void Input_TextBox_TextChanged(object sender, EventArgs e)
        {
            CheckFillTextBoxs();
        }
       


       


        

        

    }
}
